package com.yihu.wlyy.web.doctor.health;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yihu.wlyy.entity.patient.PatientHealthRecordDiet;
import com.yihu.wlyy.entity.patient.PatientHealthRecordMedication;
import com.yihu.wlyy.entity.patient.PatientHealthRecordSports;
import com.yihu.wlyy.service.app.health.PatientHealthRecordService;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.web.BaseController;

/**
 * 医生端：健康记录控制类
 * @author George
 *
 */
@Controller
@RequestMapping(value = "/doctor/health_record")
public class DoctorHealthRecordController extends BaseController {

	@Autowired
	private PatientHealthRecordService patientHealthRecordService;

	/**
	 * 患者最近填写的运动、用药、饮食内容
	 * @param patient 患者标识
	 * @return
	 */
	@RequestMapping(value = "recent")
	@ResponseBody
	public String recent(String patient) {
		try {
			JSONObject json = patientHealthRecordService.findRecentByPatient(patient);
			if (json != null) {
				return write(200, "查询成功", "data", json);
			} else {
				return error(-1, "查询失败");
			}
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败");
		}
	}
	
	/**
	 * 运动记录查询接口
	 * @param patient 患者标识
	 * @param pagesize 分页大小
	 * @return
	 */
	@RequestMapping(value = "list_sports")
	@ResponseBody
	public String sports(String patient, String sortDate, int pagesize) {
		try {
			Page<PatientHealthRecordSports> data = patientHealthRecordService.findSportsByPatient(patient, DateUtil.strToDateLong(sortDate), pagesize);
			if (data != null) {
				JSONArray array = new JSONArray();
				for (PatientHealthRecordSports record : data) {
					if (record == null) {
						continue;
					}
					JSONObject json = new JSONObject();
					json.put("id", record.getId());
					// 设置记录标识
					json.put("code", record.getCode());
					// 设置记录日期
					json.put("record_date", DateUtil.dateToStrShort(record.getRecordDate()));
					json.put("sortDate", DateUtil.dateToStrLong(record.getSortDate()));
					// 设置运动时长（分钟）
					json.put("sports_time", record.getSportsTime());
					// 设置运动强度
					json.put("sports_type", record.getSportsTypeName());
					// 设置运动类型
					json.put("sports", record.getSportsName());
					array.put(json);
				}
				return write(200, "查询成功", "list", array);
			} else {
				return error(-1, "查询失败");
			}
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败");
		}
	}

	/**
	 * 用药记录查询接口
	 * @param patient 患者标识
	 * @param pagesize 分页大小
	 * @return
	 */
	@RequestMapping(value = "list_medication")
	@ResponseBody
	public String medication(String patient, String sortDate, int pagesize) {
		try {
			Page<PatientHealthRecordMedication> data = patientHealthRecordService.findMedicationByPatient(patient, DateUtil.strToDateLong(sortDate), pagesize);
			if (data != null) {
				JSONArray array = new JSONArray();
				for (PatientHealthRecordMedication record : data) {
					if (record == null) {
						continue;
					}
					JSONObject json = new JSONObject();
					json.put("id", record.getId());
					// 设置记录标识
					json.put("code", record.getCode());
					// 设置记录日期
					json.put("record_date", DateUtil.dateToStrShort(record.getRecordDate()));
					json.put("sortDate", DateUtil.dateToStrLong(record.getSortDate()));
					// 药品名称
					json.put("medicines", record.getMedicinesName());
					array.put(json);
				}
				return write(200, "查询成功", "list", array);
			} else {
				return error(-1, "查询失败");
			}
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败");
		}
	}

	/**
	 * 饮食记录查询接口
	 * @param patient 患者标识
	 * @param page 页码
	 * @param pagesize 分页大小
	 * @return
	 */
	@RequestMapping(value = "list_diet")
	@ResponseBody
	public String diet(String patient, String sortDate, int pagesize) {
		try {
			Page<PatientHealthRecordDiet> data = patientHealthRecordService.findDietByPatient(patient, DateUtil.strToDateLong(sortDate), pagesize);
			if (data != null) {
				JSONArray array = new JSONArray();
				for (PatientHealthRecordDiet record : data) {
					if (record == null) {
						continue;
					}
					JSONObject json = new JSONObject();
					json.put("id", record.getId());
					// 设置记录标识
					json.put("code", record.getCode());
					// 设置记录日期
					json.put("record_date", DateUtil.dateToStrShort(record.getRecordDate()));
					json.put("sortDate", DateUtil.dateToStrLong(record.getSortDate()));
					// 设置饮食内容
					json.put("content", record.getContent());
					// 设置图片URL,多图以逗号分隔
					json.put("images", record.getImages());
					array.put(json);
				}
				return write(200, "查询成功", "list", array);
			} else {
				return error(-1, "查询失败");
			}
		} catch (Exception e) {
			error(e);
			return error(-1, "查询失败");
		}
	}
}
